Private Sub mnuSortingSortFields_Click()
Dim result%, jobnum%, mainjob%, fieldcount%, SortFieldN%, fieldHandle&, fieldLength%, Direction%
Dim FieldName$
result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.", vbOKOnly + vbCritical, "Serious Error"
End
End If
jobnum% = PEOpenPrintJob(lblReportName.Caption) ' Name from label on sample form
ErrorTrap "OpenPrintJob in SortingSortFields", jobnum%
' Subreport check - if a subreport is currently selected on the main form, jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubReport in SortingSortFields", mainjob%
End If
' Count how many sort fields there are
fieldcount% = PEGetNSortFields(jobnum%)
If fieldcount% =-1 Then
' Something went wrong
ErrorTrap "GetNSortFields in SortingSortFields", jobnum%
ElseIf fieldcount% = 0 Then
' None!
MsgBox "There are no sort fields defined in this report.", vbOKOnly + vbCritical, "No Sort Fields"
Else
' There are sort fields
' The sort order display form is loaded so that the list box can be filled with sort fields
Load SortOrder
CenterForm Sample, SortOrder
SortOrder.Caption = "Sort Order"
' Loop through the number of sort fields determined by PEGetNSortFields
For SortFieldN% = 0 To fieldcount% - 1
' Get the fieldhandle, length and sort direction
result% = PEGetNthSortField(jobnum%, SortFieldN%, fieldHandle&, fieldLength%, Direction%)
ErrorTrap "GetNthSortField in SortingSortFields", jobnum%
' Set spaces into the fieldname string based on the length retrieved in the preceding call
' NOTE! Failure to do this step results in General Protection Faults
FieldName$ = String$(fieldLength%, 0)
' crvbHandleToBstr fills the fieldname string with the name of the sort field
result% = crvbHandleToBstr(fieldHandle&, FieldName$, fieldLength%)
ErrorTrap "HandleToBstr for FieldName in SortingSortFields", jobnum%
FieldName$ = Trim$(FieldName$)
FieldName$ = Left$(FieldName$, Len(FieldName$) - 1)
If Direction% = PE_SF_ASCENDING Then
FieldName$ = FieldName$ & " (A)"
ElseIf Direction% = PE_SF_DESCENDING Then
FieldName$ = FieldName$ & " (D)"
Else
MsgBox "The direction flag has been set to an impossible value. Execution must stop.", vbOKOnly + vbCritical, "Fatal Error"
End
End If
' The fieldname string is then inserted into the list box on the sort order form
SortOrder!lstSortList.AddItem FieldName$
Next SortFieldN%
' Default to first item on list
SortOrder!lstSortList.ListIndex = 0
' Once the list is complete, the sort order form is shown 1 ' Modally - execution in this module will stop
' until the sort order form is hidden (by pressing Ok or Cancel)
SortOrder.Show 1 ' Modal
' Now that execution has returned, check what button was pressed and react accordingly
Select Case SortOrder.Tag
Case "Ok"
If MsgBox("Do you want to change the sort order of this print job?", vbYesNo + vbQuestion, "Change Sort Order?") = vbYes Then
' Make changes to the print job sort order - note that this has no permanent affect on the report
' Changes made at the API level cannot be stored in the report
For SortFieldN% = 0 To SortOrder!lstSortList.ListCount - 1
' Pull the direction of the sort field item first and store it
If Right$(SortOrder!lstSortList.List(SortFieldN%), 3) = "(A)" Then
Direction% = PE_SF_ASCENDING
Else
Direction% = PE_SF_DESCENDING
End If
' Copy the name of the sort field from the list box
FieldName$ = SortOrder!lstSortList.List(SortFieldN%)
' Clip the direction off and add null
FieldName$ = Left$(FieldName$, Len(FieldName$) - 4) & Chr$(0)
' Set the sort field
result% = PESetNthSortField(jobnum%, SortFieldN%, FieldName$, Direction%)
ErrorTrap "SetNthSortField in SortingSortFields", jobnum%
Next SortFieldN%
End If
Case "Cancel"
MsgBox "Cancel was pressed on the Sort Order form. The sort order of the print job will not be changed.", vbOKOnly + vbInformation, "Cancel Pressed"
End Select
' Finished with the sort order form - unload it
Unload SortOrder
' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
' Simplified version of the custom-l ink preview routine (no custom buttons)
result% = PEOutputToWindow(jobnum%, "Sort Order Demonstration Preview" & Chr$(0), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0)
ErrorTrap "OutputtoWindow in SortingSortFields", jobnum%
result% = PEStartPrintJob(jobnum%, True)
ErrorTrap "StartPrintJob in SortingSortFields", jobnum%
result% = 1
Do While result% <> 0
DoEvents
DoEvents
result% = PEGetWindowHandle(jobnum%)
Loop
End If
End If
' Close print job and engine
' Subreport check - if a subreport is currently open, call PreviewReport to offer a chance to
' preview the main report, then close the subreport and main report
If lblSubreportName.Visible Then
PreviewReport mainjob%
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubReport in SortingSortFields", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If
PECloseEngine
MsgBox "Sort Fields Complete!", vbOKOnly, "Operation Succeeded"
End Sub
ActiveX
Private Sub mnuSortingSortFields_Click()
Dim SortFieldN As Integer
Dim FieldName As String, SortDirection As String
Dim hwndPreviewWindow As Long
CrystalReport1.ReportFileName = lblReportName.Caption ' Name from label on sample form
' Set up endless loop (only ends with an Exit Do) for editing multiple sort fields
Do While True
FieldName = InputBox("Enter sort field to edit. Press Cancel to end editing of sort fields:", "Enter Sort Field Number", "0")
' if a zero length string, then Cancel was pressed, exit the loop
If FieldName = "" Then Exit Do
' Otherwise a sort field number was entered, convert it
SortFieldN = Val(FieldName)
' Get FieldName
FieldName = InputBox("Enter field name for sort field. Press Cancel to end editing of sort fields. Field names must be encased in braces:", "Enter Field Name:", "{}")
' if a zero length string, then Cancel was pressed, exit the loop
If FieldName = "" Then Exit Do
' Get Sort Direction
SortDirection = InputBox("Enter the sort direction. Press Cancel to end editing of sort fields. Press 'A' for Ascending, 'D' for Descending.", "Enter Sort Direction", "A")
If SortDirection = "" Then Exit Do
If SortDirection = "A" Then SortDirection = "+" Else SortDirection = "-"
' Load sort field with data input
CrystalReport1.SortFields(SortFieldN) = SortDirection & FieldName
Loop
' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
CrystalReport1.Destination = 0 ' Window
CrystalReport1.Action = 1 ' Print
ErrorTrap "SortingSortFields"
hwndPreviewWindow = GetActiveWindow()
Do While IsWindow(hwndPreviewWindow)
DoEvents
Loop
End If
' Close the report
CrystalReport1.ReportFileName = ""
MsgBox "Sort Fields Complete!", vbOKOnly, "Operation Completed"
End Sub
Seagate Software IMG Holdings, Inc. http://www.seagatesoftware.com Support services: http://support.seagatesoftware.com |